<template>
  <div class="newAddress">
    <van-nav-bar
      :title="aid ? '编辑地址' : '新增地址'"
      left-text="返回"
      left-arrow
      @click-left="back"
    />
    <van-address-edit
      :area-list="areaList"
      show-postal
      :show-delete="!!aid"
      show-set-default
      show-search-result
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      :address-info="addressInfo"
      @save="save"
      @delete="deleteAddress"
    />
  </div>
</template>

<script>
import "../assets/less/editAddress.less";
import areaList from "../assets/js/area";
export default {
  name: "NewAddress",
  data() {
    return {
      areaList,
      addressInfo: {},
      aid: "",
    };
  },
  created() {
    this.aid = this.$route.params.aid;
    if (this.aid) {
      this.findAddressByAid();
    }
  },
  methods: {
    back() {
      this.$router.go(-1);
    },
    deleteAddress(value) {
      let tokenString = this.$cookies.get("tokenString");
      if (!tokenString) {
        return this.$router.push({ name: "Login" });
      }
      this.$toast.loading({
        message: "加载中",
        forbidClick: true,
        duration: 0,
      });
      this.$axios({
        method: "POST",
        url: "/deleteAddress",
        data: {
          appkey: this.appkey,
          tokenString,
          aid: value.aid,
        },
      })
        .then((res) => {
        //   console.log("res deleteAddress ==> ", res);
          this.$toast.clear();

          if (res.data.code == 700) {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 1000,
            });
            return this.$router.push({ name: "Login" });
          }

          if (res.data.code === 10000) {
            this.$router.push({ name: "Address" });
          }

          this.$toast({
            message: res.data.msg,
            forbidClick: true,
            duration: 1000,
          });
        })
        .catch((err) => {
          this.$toast.clear();
          console.log("err ==> ", err);
        });
    },
    save(value) {
      if (this.aid) {
        this.edit(value);
      } else {
        this.add(value);
      }
    },
    edit(value) {
      let edit = false;
      for (let key in this.addressInfo) {
        if (this.addressInfo[key] !== value[key]) {
          edit = true;
          break;
        }
      }
      if (edit) {
        let tokenString = this.$cookies.get("tokenString");
        if (!tokenString) {
          return this.$router.push({ name: "Login" });
        }
        let addressData = {
          ...value,
          tokenString,
          appkey: this.appkey,
        };
        addressData.isDefault = Number(addressData.isDefault);
        delete addressData.country;
        this.$toast.loading({
          message: "加载中",
          forbidClick: true,
          duration: 0,
        });
        this.axios({
          method: "POST",
          url: "/editAddress",
          data: {
            appkey: this.appkey,
            tokenString,
            ...addressData,
          },
        })
          .then((res) => {
            // console.log("res edit ==> ", res);
            this.$toast.clear();
            if (res.data.code == 700) {
              this.$toast({
                message: res.data.msg,
                forbidClick: true,
                duration: 1000,
              });
              return this.$router.push({ name: "Login" });
            }

            if (res.data.code === 30000) {
              this.$router.push({ name: "Address" });
            }

            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 1000,
            });
          })
          .catch((err) => {
            this.$toast.clear();
            console.log("err ==> ", err);
          });
      } else {
        this.$router.push({ name: "Address" });
      }
    },
    add(value) {
      let tokenString = this.$cookies.get("tokenString");
      if (!tokenString) {
        return this.$router.push({ name: "Login" });
      }

      let addressData = {
        ...value,
        tokenString,
        appkey: this.appkey,
      };

      addressData.isDefault = Number(addressData.isDefault);
      delete addressData.country;
      this.$toast.loading({
        message: "加载中",
        forbidClick: true,
        duration: 0,
      });

      this.$axios({
        method: "POST",
        url: "/addAddress",
        data: addressData,
      })
        .then((res) => {
        //   console.log("res save ==> ", res);
          this.$toast.clear();

          if (res.data.code == 700) {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 1000,
            });
            return this.$router.push({ name: "Login" });
          }

          if (res.data.code === 9000) {
            this.$router.push({ name: "Address" });
            // this.$router.go(-1);
          }

          this.$toast({
            message: res.data.msg,
            forbidClick: true,
            duration: 1000,
          });
        })
        .catch((err) => {
          this.$toast.clear();
          console.log("err ==> ", err);
        });
    },
    findAddressByAid() {
      let tokenString = this.$cookies.get("tokenString");
      if (!tokenString) {
        return this.$router.push({ name: "Login" });
      }
      this.$toast.loading({
        message: "加载中",
        forbidClick: true,
        duration: 0,
      });
      this.$axios({
        method: "GET",
        url: "/findAddressByAid",
        params: {
          appkey: this.appkey,
          tokenString,
          aid: this.aid,
        },
      })
        .then((res) => {
          //   console.log("res findAddressByAid ==> ", res);
          this.$toast.clear();

          if (res.data.code == 700) {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 1000,
            });
            return this.$router.push({ name: "Login" });
          }

          if (res.data.code === 40000) {
            let data = res.data.result[0];

            data.isDefault = !!data.isDefault;

            this.addressInfo = data;
            // console.log("addressInfo-->", this.addressInfo);
          } else {
            this.$toast({
              message: res.data.msg,
              forbidClick: true,
              duration: 1000,
            });
          }
        })
        .catch((err) => {
          this.$toast.clear();
          console.log("err ==> ", err);
        });
    },
  },
};
</script>

<style lang="less" scoped>
</style>